package myTest.example

interface Expr

class Num(val value: Int) : Expr

class Sum(val left: Expr, val right: Expr) : Expr


fun eval(e: Expr): Int {

    //相当于instanceOf
    if (e is Num) {
        //强转,is后面省强转
//        val num = e as Num

        //省return
         e.value
    }

    if (e is Sum) {
        eval(e.right) +eval(e.left)
    }

    throw IllegalArgumentException("非法表达式")
}


fun main(args: Array<String>) {
    //1+2+4
    println(
    eval(Sum(Sum(Num(1),Num(2)),Num(4)))
    )
}